

/**
 *计算两个坐标点之间的距离
 *
 * @export
 * @param {Number} x1 坐标点 1X 值
 * @param {Number} y1 坐标点 1Y 值
 * @param {Number} x2 坐标点 2X 值
 * @param {Number} y2 坐标点 2Y 值
 * @return {Number} 距离值
 */
export function distance(x1, y1, x2, y2) {
    var dx = x1 - x2,
        dy = y1 - y2;
    return Math.sqrt(dx * dx + dy * dy);
};


/**
 *记录算两个坐标点之间的角度值
 *
 * @export
 * @param {Number} x1 坐标点 1X 值
 * @param {Number} y1 坐标点 1Y 值
 * @param {Number} x2 坐标点 2X 值
 * @param {Number} y2 坐标点 2Y 值
 * @return {Number} 距离值
 */
export function angle(x1, y1, x2, y2) {
    return Math.atan2(y2 - y1, x2 - x1);
}

/**
 * 计算复数元素长宽位置
 *
 * @export
 * @param {Array} ItemArray 多个几何图形对象
 * @return {Object} 外框大小
 */
export function getItemOptions(ItemArray) {
    var leftArray = [], rightArray = [], topArray = [], bottomArray = [], xArray = [], yArray = [];
    for (let i = 0; i < ItemArray.length; i++) {
        const element = ItemArray[i].getValue();
        leftArray.push(element.x);
        rightArray.push(element.x + element.width);
        topArray.push(element.y);
        bottomArray.push(element.y + element.height);
        xArray.push(element.x);
        yArray.push(element.y);
    }
    var x = Math.min.apply(null, xArray);
    var y = Math.min.apply(null, yArray);
    var width = Math.max.apply(null, rightArray) - Math.min.apply(null, leftArray);
    var height = Math.max.apply(null, bottomArray) - Math.min.apply(null, topArray);
    return {
        x, y, width, height,
    }
}


/**
 * 矩形碰撞检测
 *
 * @export
 * @param {Object} r1 矩形1
 * @param {Object} r2 矩形2
 * @return {Blob}} 
 */
export function hitRectangle(r1, r2) {
    let hit, combinedHalfWidths, combinedHalfHeights, vx, vy;
    hit = false;

    r1.centerX = r1.x + r1.width / 2;
    r1.centerY = r1.y + r1.height / 2;
    r2.centerX = r2.x + r2.width / 2;
    r2.centerY = r2.y + r2.height / 2;

    r1.halfWidth = r1.width / 2;
    r1.halfHeight = r1.height / 2;
    r2.halfWidth = r2.width / 2;
    r2.halfHeight = r2.height / 2;

    vx = r1.centerX - r2.centerX;
    vy = r1.centerY - r2.centerY;

    combinedHalfWidths = r1.halfWidth + r2.halfWidth;
    combinedHalfHeights = r1.halfHeight + r2.halfHeight;

    if (Math.abs(vx) < combinedHalfWidths) {
        if (Math.abs(vy) < combinedHalfHeights) {
            hit = true;
        } else {
            hit = false;
        }
    } else {
        hit = false;
    }
    return hit;
};